package org.csu.mypetstorems.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.csu.mypetstorems.entity.Admin;
import org.csu.mypetstorems.entity.Category;
import org.csu.mypetstorems.entity.PageBean;
import org.csu.mypetstorems.mapper.AdminMapper;
import org.csu.mypetstorems.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("adminService")
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Boolean signOn(String username, String password) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        Admin admin = adminMapper.selectOne(queryWrapper);
        return admin != null && admin.getPassword().equals(password);
    }

    @Override
    public Boolean addAdmin(Admin admin) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", admin.getUsername());
        if(adminMapper.selectOne(queryWrapper) != null) {
            return false;
        }
        admin.setPassword("123456");
        return adminMapper.insert(admin) > 0;
    }

    @Override
    public Boolean updateAdmin(Admin admin) {
        return adminMapper.updateById(admin) > 0;
    }

    @Override
    public Boolean deleteAdmin(String Id) {
        return adminMapper.deleteById(Id) > 0;
    }

    @Override
    public PageBean<Admin> getAdminWithCondition(Integer pageNum, Integer pageSize, String nameForSearch) {
        PageBean<Admin> pageBean = new PageBean<>();
        PageHelper.startPage(pageNum, pageSize);
        List<Admin> adminList = adminMapper.getAdminWithCondition(nameForSearch);
        Page<Admin> page = (Page<Admin>) adminList;
        pageBean.setTotal(page.getTotal());
        pageBean.setList(page.getResult());
        return pageBean;
    }
}